package com.meilingcloud.biobank.controller;

import com.meilingcloud.biobank.domain.SampleGroup;
import com.meilingcloud.biobank.service.ISampleGroupService;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 样本组 Controller
 *
 * @author chenl
 * @date 2021-03-02
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/biobank/sampleGroup")
public class SampleGroupController extends BaseController {

    private final ISampleGroupService iSampleGroupService;

    /**
     * 查询样本组 列表
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleGroup:list')")
    @GetMapping("/list")
    public AjaxResult list(SampleGroup sampleGroup) {
        List<SampleGroup> list = iSampleGroupService.queryList(sampleGroup);
        return AjaxResult.success(list);
    }

    /**
     * 获取样本组 详细信息
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleGroup:query')")
    @GetMapping(value = "/{sampGroupId}")
    public AjaxResult getInfo(@PathVariable("sampGroupId") Long sampGroupId) {
        return AjaxResult.success(iSampleGroupService.queryById(sampGroupId));
    }

    /**
     * 新增样本组
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleGroup:add')")
    @Log(title = "样本组 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SampleGroup sampleGroup) {
        sampleGroup.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iSampleGroupService.save(sampleGroup));
    }

    /**
     * 修改样本组
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleGroup:edit')")
    @Log(title = "样本组 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody SampleGroup sampleGroup) {
        sampleGroup.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSampleGroupService.updateSampGroupById(sampleGroup));
    }

    /**
     * 删除样本组
     */
    @PreAuthorize("@ss.hasPermi('biobank:sampleGroup:remove')")
    @Log(title = "样本组 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sampGroupIds}")
    public AjaxResult remove(@PathVariable Long[] sampGroupIds) {
        return toAjax(iSampleGroupService.removeSampGroupByIds(Arrays.asList(sampGroupIds)));
    }
}
